package com.ruoyi.web.controller.insect.controller;

import java.util.Arrays;
import java.util.List;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.insect.domain.CropDiseaseMapping;
import com.ruoyi.insect.service.ICropDiseaseMappingService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

import javax.servlet.http.HttpServletResponse;

/**
 * 作物病虫害映射Controller
 */
@RestController
@RequestMapping("/insect/cropdisease")
public class CropDiseaseMappingController extends BaseController {
    @Autowired
    private ICropDiseaseMappingService cropDiseaseMappingService;

    /**
     * 查询作物病虫害映射列表
     */
    @PreAuthorize("@ss.hasPermi('insect:cropdisease:list')")
    @GetMapping("/list")
    public TableDataInfo list(CropDiseaseMapping cropDiseaseMapping) {
        startPage();
        List<CropDiseaseMapping> list = cropDiseaseMappingService.selectCropDiseaseMappingList(cropDiseaseMapping);
        return getDataTable(list);
    }

    /**
     * 导出作物病虫害映射列表
     */
    @PreAuthorize("@ss.hasPermi('insect:cropdisease:export')")
    @Log(title = "作物病虫害映射", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CropDiseaseMapping cropDiseaseMapping) {
        List<CropDiseaseMapping> list = cropDiseaseMappingService.selectCropDiseaseMappingList(cropDiseaseMapping);
        ExcelUtil<CropDiseaseMapping> util = new ExcelUtil<CropDiseaseMapping>(CropDiseaseMapping.class);
        util.exportExcel(response, list, "作物病虫害映射数据");
    }

    /**
     * 获取作物病虫害映射详细信息
     */
    @PreAuthorize("@ss.hasPermi('insect:cropdisease:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(cropDiseaseMappingService.selectCropDiseaseMappingById(id));
    }

    /**
     * 新增作物病虫害映射
     */
    @PreAuthorize("@ss.hasPermi('insect:cropdisease:add')")
    @Log(title = "作物病虫害映射", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CropDiseaseMapping cropDiseaseMapping) {
        return toAjax(cropDiseaseMappingService.insertCropDiseaseMapping(cropDiseaseMapping));
    }

    /**
     * 修改作物病虫害映射
     */
    @PreAuthorize("@ss.hasPermi('insect:cropdisease:edit')")
    @Log(title = "作物病虫害映射", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CropDiseaseMapping cropDiseaseMapping) {
        return toAjax(cropDiseaseMappingService.updateCropDiseaseMapping(cropDiseaseMapping));
    }

    /**
     * 删除作物病虫害映射
     */
    @PreAuthorize("@ss.hasPermi('insect:cropdisease:remove')")
    @Log(title = "作物病虫害映射", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(cropDiseaseMappingService.deleteCropDiseaseMappingByIds(ids));
    }

    /**
     * 根据作物名称获取病虫害列表
     */
    @GetMapping("/getDiseasesByCrop")
    public AjaxResult getDiseasesByCrop(String cropName) {
        try {
            List<String> diseases = cropDiseaseMappingService.getDiseaseListByCrop(cropName);
            return AjaxResult.success(diseases);
        } catch (Exception e) {
            logger.error("获取病虫害列表失败", e);
            return AjaxResult.error("获取病虫害列表失败");
        }
    }

    /**
     * 获取所有作物名称
     */
    @GetMapping("/getAllCropNames")
    public AjaxResult getAllCropNames() {
        // 这里可以从数据库获取，或者返回固定的作物列表
        List<String> cropNames = Arrays.asList("水稻", "小麦", "玉米", "大豆", "蔬菜");
        return AjaxResult.success(cropNames);
    }

    /**
     * 验证作物和病虫害映射是否存在
     */
    @GetMapping("/checkMapping")
    public AjaxResult checkMapping(String cropName, String diseaseName) {
        try {
            boolean exists = cropDiseaseMappingService.checkCropDiseaseMapping(cropName, diseaseName);
            return AjaxResult.success(exists);
        } catch (Exception e) {
            logger.error("验证映射关系失败", e);
            return AjaxResult.error("验证映射关系失败");
        }
    }
}
